package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;

    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;

    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;

    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;

    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;

    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    @Override
    public int saveCreateClue(Clue clue) {
        return clueMapper.insertClue(clue);
    }

    @Override
    public List<Clue> queryClueByConditionForPage(Map<String, Object> map) {
        return clueMapper.selectClueByConditionForPage(map);
    }

    @Override
    public int queryCountOfClueByConditionForPage(Map<String, Object> map) {
        return clueMapper.selectCountOfClueByConditionForPage(map);
    }

    @Override
    public int deleteClueByIds(String[] ids) {
        return clueMapper.deleteClueByIds(ids);
    }

    @Override
    public Clue queryClueById(String id) {
        return clueMapper.selectClueById(id);
    }

    @Override
    public int updateClueById(Clue clue) {
        return clueMapper.updateClueById(clue);
    }

    @Override
    public Clue queryClueByIdForDetail(String id) {
        return clueMapper.selectClueByIdForDetail(id);
    }

    @Override
    public boolean doConvertClue(Map<String,Object> map) {
        User user = (User)map.get(Contants.SESSION_USER);
        String clueId = (String)map.get("clueId");
        String isCreateTransaction = (String)map.get("isCreateTransaction");
        String amountOfMoney = (String)map.get("amountOfMoney");
        String tradeName = (String)map.get("tradeName");
        String expectedClosingDate = (String)map.get("expectedClosingDate");
        String stage = (String)map.get("stage");
        String activityId = (String)map.get("activityId");

        boolean flag = true;

        try {
            Clue clue = clueMapper.selectClueById(clueId);
            Customer customer = new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(user.getId());
            customer.setName(clue.getCompany());
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());
            customer.setCreateTime(DateUtils.formatDataTime(new Date()));
            customer.setCreateBy(user.getId());
            customer.setContactSummary(clue.getContactSummary());
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setDescription(clue.getDescription());
            customer.setAddress(clue.getAddress());

            customerMapper.insertCustomer(customer);

            Contacts contacts = new Contacts();
            contacts.setId(UUIDUtils.getUUID());
            contacts.setOwner(user.getId());
            contacts.setSource(clue.getSource());
            contacts.setCustomerId(customer.getId());
            contacts.setFullname(clue.getFullname());
            contacts.setAppellation(clue.getAppellation());
            contacts.setEmail(clue.getEmail());
            contacts.setMphone(clue.getMphone());
            contacts.setJob(clue.getJob());
            contacts.setCreateBy(user.getId());
            contacts.setCreateTime(DateUtils.formatDataTime(new Date()));
            contacts.setDescription(clue.getDescription());
            contacts.setContactSummary(clue.getContactSummary());
            contacts.setNextContactTime(clue.getNextContactTime());
            contacts.setAddress(clue.getAddress());

            contactsMapper.insertContacts(contacts);

            List<ClueRemark> clueRemarkList = clueRemarkMapper.selectAllClueRemarkByClueId(clueId);

            if(clueRemarkList.size()!=0){

                List<CustomerRemark> customerRemarkList = new ArrayList<>();
                List<ContactsRemark> contactsRemarkList = new ArrayList<>();

                CustomerRemark customerRemark = null;
                ContactsRemark contactsRemark = null;

                for(int i = 0;i < clueRemarkList.size();i++){
                    customerRemark = new CustomerRemark();
                    contactsRemark = new ContactsRemark();

                    customerRemark.setId(UUIDUtils.getUUID());
                    customerRemark.setNoteContent(clueRemarkList.get(i).getNoteContent());
                    customerRemark.setCustomerId(customer.getId());
                    customerRemark.setCreateBy(clueRemarkList.get(i).getCreateBy());
                    customerRemark.setCreateTime(clueRemarkList.get(i).getCreateTime());
                    customerRemark.setEditBy(clueRemarkList.get(i).getEditBy());
                    customerRemark.setEditTime(clueRemarkList.get(i).getEditTime());
                    customerRemark.setEditFlag(clueRemarkList.get(i).getEditFlag());

                    customerRemarkList.add(customerRemark);

                    contactsRemark.setId(UUIDUtils.getUUID());
                    contactsRemark.setNoteContent(clueRemarkList.get(i).getNoteContent());
                    contactsRemark.setContactsId(contacts.getId());
                    contactsRemark.setCreateBy(clueRemarkList.get(i).getCreateBy());
                    contactsRemark.setCreateTime(clueRemarkList.get(i).getCreateTime());
                    contactsRemark.setEditBy(clueRemarkList.get(i).getEditBy());
                    contactsRemark.setEditTime(clueRemarkList.get(i).getEditTime());
                    contactsRemark.setEditFlag(clueRemarkList.get(i).getEditFlag());

                    contactsRemarkList.add(contactsRemark);
                }

                customerRemarkMapper.insertCustomerRemarks(customerRemarkList);
                contactsRemarkMapper.insertContactsRemarks(contactsRemarkList);
            }

            List<ClueActivityRelation> clueActivityRelationList = clueActivityRelationMapper.selectClueActivityRelationByClueId(clueId);

            if(clueActivityRelationList.size()!=0){
                List<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();

                ContactsActivityRelation contactsActivityRelation = null;

                for(int i = 0;i < clueActivityRelationList.size();i++){
                    contactsActivityRelation = new ContactsActivityRelation();
                    contactsActivityRelation.setId(UUIDUtils.getUUID());
                    contactsActivityRelation.setContactsId(contacts.getId());
                    contactsActivityRelation.setActivityId(clueActivityRelationList.get(i).getActivityId());

                    contactsActivityRelationList.add(contactsActivityRelation);
                }

                contactsActivityRelationMapper.insertContactsActivityRelations(contactsActivityRelationList);
            }

            if("true".equals(isCreateTransaction)){
                Tran tran = new Tran();
                tran.setId(UUIDUtils.getUUID());
                tran.setOwner(user.getId());
                tran.setMoney(amountOfMoney);
                tran.setName(tradeName);
                tran.setExpectedDate(expectedClosingDate);
                tran.setCustomerId(customer.getId());
                tran.setStage(stage);
                tran.setSource(clue.getSource());
                tran.setActivityId(activityId);
                tran.setContactsId(contacts.getId());
                tran.setCreateBy(user.getId());
                tran.setCreateTime(DateUtils.formatDataTime(new Date()));
                tran.setDescription(clue.getDescription());
                tran.setContactSummary(clue.getContactSummary());
                tran.setNextContactTime(clue.getNextContactTime());

                tranMapper.insertTran(tran);

                if(clueRemarkList.size()!=0){
                    List<TranRemark> tranRemarkList = new ArrayList<>();

                    TranRemark tranRemark = null;

                    for(int i = 0;i < clueRemarkList.size();i++){
                        tranRemark = new TranRemark();

                        tranRemark.setId(UUIDUtils.getUUID());
                        tranRemark.setNoteContent(clueRemarkList.get(i).getNoteContent());
                        tranRemark.setTranId(tran.getId());
                        tranRemark.setCreateBy(clueRemarkList.get(i).getCreateBy());
                        tranRemark.setCreateTime(clueRemarkList.get(i).getCreateTime());
                        tranRemark.setEditBy(clueRemarkList.get(i).getEditBy());
                        tranRemark.setEditTime(clueRemarkList.get(i).getEditTime());
                        tranRemark.setEditFlag(clueRemarkList.get(i).getEditFlag());

                        tranRemarkList.add(tranRemark);
                    }

                    tranRemarkMapper.insertTranRemarks(tranRemarkList);
                }
            }else{

            }

            //删除线索备注
            clueRemarkMapper.deleteClueRemarkById(clueId);
            //删除线索活动关系
            clueActivityRelationMapper.deleteAllClueActivityRelationByClueId(clueId);
            //删除线索
            clueMapper.deleteClueById(clueId);

        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }

        return flag;
    }
}
